//
//  PickerView.cpp
//  client
//
//  Created by DuanHongbo on 15/12/30.
//
//

#include "PickerView.hpp"


NS_CC_BEGIN

namespace ui {
    
PickerView* PickerView::create()
{
    PickerView* widget = new (std::nothrow) PickerView();
    if (widget && widget->init())
    {
        widget->autorelease();
        return widget;
    }
    CC_SAFE_DELETE(widget);
   
    return nullptr;
}

bool PickerView::init()
{
    if (ListView::init())
    {
        _pickedIndex=0;
        this->setBounceEnabled(false);
        ScrollView::addEventListener([this](Ref* sender,ScrollView::EventType event){
            if(event==ScrollView::EventType::AUTOSCROLL_STOP){
                //this->onAutoScellStop();
				this->runAction(Sequence::create(
					DelayTime::create(0.2),
					CallFunc::create(CC_CALLBACK_0(PickerView::onAutoScellStop, this)),
					nullptr
					)
					);
            }
        });
        return true;
    }
    return false;
}
    
void PickerView::onEnter()
{
    ListView::onEnter();
    jumpToPicked();
}
    
int PickerView::getPickedIndex()
{
    return _pickedIndex;
}
    
void PickerView::addEventListener(const ccPickerViewCallback& callback)
{
    _pickerViewCallback = callback;
}

void PickerView::pickFirst()
{
    doLayout();
    _direction==Direction::VERTICAL?jumpToTop():jumpToLeft();
    _pickedIndex=0;
    jumpToPicked();
}
    
void PickerView::pickLast()
{
    doLayout();
    _direction==Direction::VERTICAL?jumpToBottom():jumpToRight();
    _pickedIndex=getItems().size()-1;
    jumpToPicked();
}
 
void PickerView::pickTo(int index)
{
    doLayout();
    _direction==Direction::VERTICAL?jumpToBottom():jumpToRight();
    if (index-1<getItems().size()&&index>=0){
        _pickedIndex=index-1;
    }else{
        
    }

    jumpToPicked();
    
    return;
}
    
    
void PickerView::jumpToPicked()
{
    //float containerPos = (_direction==Direction::VERTICAL?getInnerContainerPosition().y:getInnerContainerPosition().x);
    float containerSize = (_direction==Direction::VERTICAL?getInnerContainerSize().height:getInnerContainerSize().width);
    int itemCount = getItems().size();
    int unitLength = containerSize/itemCount;
    float finalPos = (itemCount-_pickedIndex-1)*unitLength;
    Vec2 moveToPos;
    moveToPos.x = (_direction==Direction::VERTICAL?getInnerContainerPosition().x:finalPos);
    moveToPos.y = (_direction==Direction::HORIZONTAL?getInnerContainerPosition().y:finalPos*-1);
    jumpToDestination(moveToPos);
}
    
void PickerView::onAutoScellStop()
{

    float containerPos = (_direction==Direction::VERTICAL?getInnerContainerPosition().y:getInnerContainerPosition().x);
    float containerSize = (_direction==Direction::VERTICAL?getInnerContainerSize().height:getInnerContainerSize().width);
    int itemCount = getItems().size();
    if(itemCount>0){
        int unitLength = containerSize/itemCount;
        int halfUnitLength = unitLength*0.5f;
        int curPos = (int)containerPos;
        int d = curPos%unitLength;
        int idx = 0;
        if(abs(d)<halfUnitLength)
            idx = curPos/unitLength;
        else
            idx = (curPos/unitLength)+(curPos<0?-1:1);
        
        float finalPos = idx*unitLength;
        
        Vec2 moveToPos;
        moveToPos.x = (_direction==Direction::VERTICAL?getInnerContainerPosition().x:finalPos);
        moveToPos.y = (_direction==Direction::HORIZONTAL?getInnerContainerPosition().y:finalPos);
        jumpToDestination(moveToPos);
        
        _pickedIndex = itemCount-std::abs(idx)-1;
        if(_pickerViewCallback){
            _pickerViewCallback(this,PickerView::EventType::ON_PICKED_ITEM,_pickedIndex);
        }
        //CCLOG("_pickedIndex %d",_pickedIndex);
    }
}
}

NS_CC_END
